import assemble_results_predict
import os
import assemble_results_nfolds
from kaggle_find_best_epoch import find_best_epoch_v15, find_best_epoch_v9
import label_candidates
import shutil


cddnames = ['spie_candidates_v4.pkl', 'kaggle_testset_candidates_v4.pkl', 'kaggle_beni_candidates_v4.pkl']
for cddname in cddnames:
    assemble_results_predict.main(
        candidate_file='../../data/tmp_fp_reduction/'+cddname,
        result_files=[
            '../../data/tmp_fp_reduction/kaggle_results/cddv9_fold{i}_{cddname}/cddv9_fold{i}_{cddname}.pkl'.format(i=i, cddname=os.path.splitext(cddname)[0]) for i in range(5)
        ],
        outfile='../../data/tmp_fp_reduction/3dcnn_'+cddname,
    )
label_candidates.main(
    infile='../../data/tmp_fp_reduction/3dcnn_spie_candidates_v4.pkl',
    outfile='../../data/reduced_candidates/3dcnn_spie_candidates_v4_newlabel.pkl',
    pos_labelinfo_file='spie_labelinfo.pkl'
)
label_candidates.main(
    infile='../../data/tmp_fp_reduction/kaggle_testset_candidates_v4.pkl',
    outfile='../../data/reduced_candidates/3dcnn_kaggle_testset_candidates_v4_newlabel.pkl',
    pos_labelinfo_file='lidc_kaggle_labelinfo_3.pkl',
    mid_labelinfo_file='lidc_kaggle_labelinfo_1.pkl',
)
shutil.copy(
    src='../../data/tmp_fp_reduction/kaggle_beni_candidates_v4.pkl',
    dst='../../data/reduced_candidates/3dcnn_kaggle_beni_candidates_v4.pkl'
)


cddnames = ['kaggle_beni_candidates_v10.pkl', 'kaggle_testset_candidates_v10.pkl', 'spie_candidates_v10.pkl']
for cddname in cddnames:
    assemble_results_predict.main(
        candidate_file='../../data/tmp_fp_reduction/' + cddname,
        result_files=[
            '../../data/tmp_fp_reduction/kaggle_results/cddv15_v2_fold{i}_{cddname}/cddv15_v2_fold{i}_{cddname}.pkl'.format(
                i=i, cddname=
                os.path.splitext(cddname)[0]) for i in range(4)
            ],
        outfile='../../data/reduced_candidates/3dcnn_cddv15_v2_' + cddname,
    )


result_files = [
        '../../data/tmp_fp_reduction/kaggle_results/cddv9_valid{i}/epoch-{epoch:04d}.pkl'.format(i=i, epoch=int(find_best_epoch_v9('../../data/tmp_fp_reduction/kaggle_log/cddv9_valid{i}.log'.format(i=i))))
        for i in range(5)
]
print(result_files)
assemble_results_nfolds.main(
    candidate_file='../../data/tmp_fp_reduction/lidc_kaggle_candidates_v9.pkl',
    result_files=result_files,
    valid_folds_lst=[
        [0],
        [1],
        [2],
        [3],
        [4],
    ],
    info_file='lidc_kaggle_info_v2.pkl',
    outfile='../../data/tmp_fp_reduction/3dcnn_candidates_list_v9.pkl',
)
label_candidates.main(
    infile='../../data/tmp_fp_reduction/3dcnn_candidates_list_v9.pkl',
    outfile='../../data/reduced_candidates/3dcnn_candidates_list_v9_newlabel.pkl',
    pos_labelinfo_file='lidc_kaggle_labelinfo_3.pkl',
    mid_labelinfo_file='lidc_kaggle_labelinfo_1.pkl',
)


result_files = [
        '../../data/tmp_fp_reduction/kaggle_results/cddv15_v2_valid{i}/epoch-{epoch:04d}.pkl'.format(i=i, epoch=int(
            find_best_epoch_v15('../../data/tmp_fp_reduction/kaggle_log/cddv15_v2_valid{i}.log'.format(i=i))))
        for i in range(4)
        ]
print(result_files)
assemble_results_nfolds.main(
    candidate_file='../../data/tmp_fp_reduction/lidc_kaggle_candidates_v15.pkl',
    result_files=result_files,
    valid_folds_lst=[
        [0],
        [1],
        [2],
        [3],
    ],
    info_file='lidc_kaggle_info_v4.pkl',
    outfile='../../data/reduced_candidates/3dcnn_cddv15_v2_lidc_kaggle_candidates_v15.pkl',
)